package com.freecloud.spring.rebbitmq;

import com.freecloud.BaseTest;
import com.freecloud.framework.utils.RabbitMqUtil;
import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;

/**
 * @Author: maomao
 * @Date: 2019-09-04 18:29
 */
public class SendRebbitMq extends BaseTest {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Test
    public void sendMessage(){

        MessageBuilder<String> message;

        for(int i = 0;i < 200;i++){
            message = MessageBuilder.withPayload("message " + i).setHeader("x-delay",10000);
            logger.info("sendMessage : {}" , message.toString());
            //freeCloudMQ.output().send(message.build());
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void sendMessaegByTemp(){
        String message;
        for (int i = 0; i < 20; i++) {
            message = "temp message " + i;
            logger.info("sendMessaegByTemp : {}",message);
            rabbitTemplate.convertAndSend(FreeCloudMQ.OUT_NAME,message);
        }
    }

    @Test
    public void sendDelayMessage(){
        String message;
        for (int i = 0; i < 2000; i++) {

            message = "temp message " + i;
            logger.info("sendMessaegByTemp : {}",message);
            RabbitMqUtil.sendDelayMessage(FreeCloudMQ.OUT_NAME, message,10);
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
